package com.dyoud.api.message.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dyoud.common.api.model.City;
import com.dyoud.common.api.service.CityService;
import com.dyoud.core.DozerUtils;
import com.dyoud.api.message.dto.MessageDto;
import com.dyoud.message.api.model.Message;
import com.dyoud.message.api.model.ResultMessage;
import com.dyoud.message.api.service.MessageService;
import com.dyoud.shop.api.model.ShopInfo;
import com.dyoud.shop.api.query.ShopPageQuery;
import com.dyoud.shop.api.request.ShopInfoRequest;
import com.dyoud.shop.api.service.ShopInfoService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/***
 * 
 * @author wangshuaifei
 * @date 2018/03/02
 */
@Service
public class MessageApiService {

	@Reference//(url = "dubbo://localhost:8002")
	private MessageService messageService;

	/**
	 * 吐槽(留言消息)
	 * 
	 * @param request
	 * @return
	 */
	public boolean addUserComment(MessageDto dto) {
		Message message = new Message();
		DozerUtils.copy(dto, message);
		return messageService.addUserComment(message) > 0;
	}
	
	/**
	 * 新增留言
	 * @param dto
	 * @return
	 */
	public boolean addUserCommentModify(MessageDto dto) {
		
		Message msg = new Message();
		DozerUtils.copy(dto, msg);
		return messageService.addUserCommentModify(msg) > 0;
	}

	/**
	 * 查询系统消息(0:店铺的系统消息.1:用户的系统消息)
	 *
	 */
	public List<ResultMessage> selectSystemMessage(MessageDto dto) {
		// TODO Auto-generated method stub
		Message mes = new Message();
		DozerUtils.copy(dto, mes);
		List<ResultMessage> selectSystemMessage = messageService.selectSystemMessage(mes);
		return selectSystemMessage;
	}

	/**
	 * 查看收益权
	 * 
	 * @author Administrator
	 *
	 */
	public List<ResultMessage> selectIncome(MessageDto dto) {
		Message mes = new Message();
		DozerUtils.copy(dto, mes);
		List<ResultMessage> selectIncome = messageService.selectIncome(mes);
		return selectIncome;
	}

	/**
	 * 查看留言回复消息
	 * 
	 * @author Administrator
	 *
	 */
	public List<ResultMessage> selectReply(MessageDto dto) {
		// TODO Auto-generated method stub
		Message mes = new Message();
		DozerUtils.copy(dto, mes);
		return messageService.selectReply(mes);
	}

	/**
	 * 根据唯一key查询消息
	 * 
	 * @author Administrator
	 *
	 */
	public String selectByOnlyKey(MessageDto dto) {
		// TODO Auto-generated method stub
		Message mes = new Message();
		DozerUtils.copy(dto, mes);
		return messageService.selectByOnlyKey(mes);
	}

	/**
	 * 根据userId或者shopId查询消息
	 * 
	 * @author Administrator
	 *
	 */
	public String selectByUserIdOrShopId(MessageDto dto) {
		// TODO Auto-generated method stub
		Message mes = new Message();
		DozerUtils.copy(dto, mes);
		return messageService.selectByUserIdOrShopId(mes);
	}

	/**
	 * 获取消息列表
	 * @param userType
	 * @param type
	 * @param page
	 * @return
	 */
	public Map<String, Object> listMessage(int userType, int type, int pageNo,long userId) {
		
		Map<String, Object> returnMap = new HashMap<>();
		Message msg = new Message();
		msg.setUserType((byte)userType);
		msg.setType((byte)type);
		
		if (userType == 0) {
			msg.setShopId(userId);
		}else if (userType == 1) {
			msg.setUserId(userId);
		}else {
			returnMap.put("error", "userType illegal.");
			return returnMap;
		}
		return messageService.listMessageByUserTypeAndType(msg,pageNo);
	}

	/**
	 * 获取未读消息数量
	 * @param type
	 * @param userType
	 * @param userId
	 * @return
	 */
	public List<Map<String, Object>> countUnreadMsg(int userType, long userId) {
		
		List<Map<String, Object>> list = new ArrayList<>();
		Map<String, Object> result = new HashMap<>();
		// 用户类型:0:商家,1:用户
		Message msg = new Message();
		msg.setUserType((byte)userType);
		if (userType == 0) {
			msg.setShopId(userId);
		}else if (userType == 1) {
			msg.setUserId(userId);
		}else {
			result.put("error", "userType illegal.");
			list.add(result);
			return list;
		}
		return messageService.countUnreadMsg(msg);
	}

	public boolean setToRead(long msgId) {
		
		int result = messageService.setToRead(msgId);
		return result == 1 ? true : false;
	}

}